package org.xaviera.test;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class AddToIndex {

	private static Map<Integer, Map<String, Set<String>>> sizeMap = new HashMap<Integer, Map<String,Set<String>>>();
	
	
	public static void main(String[] args) throws Exception {
		
		Scanner scanner = 
			new Scanner(
				new File("D:\\Temp\\test2.txt")
			).useDelimiter("[^\\p{L}\\p{Digit}]+");
		
		while (scanner.hasNext()) {
			processWord(scanner.next());
		}
		
		for (Map.Entry<Integer, Map<String, Set<String>>> sizeEntry : sizeMap.entrySet()) {
			System.out.println();
			System.out.println("------------");
			System.out.println(sizeEntry.getKey() + " symbols(s)");
			System.out.println("------------");
			System.out.println();
			for (Map.Entry<String, Set<String>> wordEntry : sizeEntry.getValue().entrySet()) {
				System.out.println(wordEntry.getKey() + " (" + wordEntry.getValue().size() + ")");
				for (String word : wordEntry.getValue()) {
					System.out.println("\t - " + word);
				}
			}
		}
	}
	
	
	private static void processWord(String word) {
		
		Integer wordLength = word.length();
		
		Map<String, Set<String>> wordMap = sizeMap.get(wordLength);
		if (wordMap == null) {
			wordMap = new HashMap<String, Set<String>>();
			sizeMap.put(wordLength, wordMap);
		}
		
		SortedSet<Character> symbolSet = new TreeSet<Character>();
		
		for (char symbol : word.toLowerCase().toCharArray()) {
			symbolSet.add(symbol);
		}
		
		int index = 0;
		char[] symbolArray = new char[symbolSet.size()];
		for (Character symbol : symbolSet) {
			symbolArray[index ++] = symbol;
		}
		
		String wordKey = new String(symbolArray);
		
		Set<String> wordSet = wordMap.get(wordKey);
		if (wordSet == null) {
			wordSet = new HashSet<String>();
			wordMap.put(wordKey, wordSet);
		}
		
		wordSet.add(word);
	}

}
